package io.renren.modules.sp.controller.front;

import io.renren.common.annotation.LogOperation;
import io.renren.common.exception.ErrorCode;
import io.renren.common.redis.RedisUtils;
import io.renren.common.sms.AliyunSms;
import io.renren.common.utils.CheckCodeUtils;
import io.renren.common.utils.Result;
import io.renren.modules.oss.cloud.OSSFactory;
import io.renren.modules.oss.entity.SysOssEntity;
import io.renren.modules.oss.service.SysOssService;
import io.renren.modules.security.user.SecurityUser;
import io.renren.modules.sp.dto.SpUserDTO;
import io.renren.modules.sp.service.SpUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
* 上传文件
*
* @author whc 15953295779@126.com
* @since 3.0 2023-04-21
*/
@RestController
@RequestMapping("front")
@Api(tags="上传文件")
public class UploadController {

    @Autowired
    private SysOssService sysOssService;

    /**
     * 前台上传
     * @param file
     * @return
     * @throws Exception
     */
    @PostMapping("upload")
    @ApiOperation(value = "上传文件")
    public Result<Map<String, Object>> upload(@RequestParam("file") MultipartFile file) throws Exception {
        if (file.isEmpty()) {
            return new Result<Map<String, Object>>().error(ErrorCode.UPLOAD_FILE_EMPTY);
        }

        //上传文件
        String extension = FilenameUtils.getExtension(file.getOriginalFilename());
        String url = OSSFactory.build().uploadSuffix(file.getBytes(), extension);

        //保存文件信息
        SysOssEntity ossEntity = new SysOssEntity();
        ossEntity.setUrl(url);
        ossEntity.setCreateDate(new Date());
        sysOssService.insert(ossEntity);

        Map<String, Object> data = new HashMap<>(1);
        data.put("src", url);
        if("PDF".equals(extension.toUpperCase())) {
            data.put("showSrc", "http://img.fii-cc.com/fii/20230604/b2f13f0061e14463b63683f3998f055a.jpg");
        }
        return new Result<Map<String, Object>>().ok(data);
    }

    @GetMapping("/download")
    public void downloadImage(String imageUrl, String fileName, HttpServletResponse response) throws IOException {
        // 设置下载的文件名称
        if(StringUtils.isNotBlank(fileName)) {
            fileName = URLEncoder.encode(fileName, "UTF-8");
            response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
        } else {
            String imageName = imageUrl.substring(imageUrl.lastIndexOf("/") + 1);
            response.setHeader("Content-Disposition", "attachment; filename=" + imageName);
        }

        // 打开连接并获取输入流
        URL url = new URL(imageUrl);
        URLConnection connection = url.openConnection();
        InputStream inputStream = connection.getInputStream();

        // 设置下载的文件类型
        response.setContentType(connection.getContentType());
        // 设置下载的文件长度
        response.setContentLength(connection.getContentLength());

        // 将输入流内容写入响应流
        FileCopyUtils.copy(inputStream, response.getOutputStream());
    }

}
